<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-icon">
        <i class="fas fa-lock"></i>
      </div>
      <h2 class="login-title">欢迎登录</h2>
      <p class="login-subtitle">请填写您的账号信息</p>
      <form @submit.prevent="login">
        <div class="input-group">
          <i class="fas fa-user"></i>
          <input v-model="username" type="text" placeholder="账号" required />
        </div>
        <div class="input-group">
          <i class="fas fa-key"></i>
          <input v-model="password" type="password" placeholder="密码" required />
        </div>
        <div class="login-options">
          <label class="remember-label">
            <input type="checkbox" v-model="remember" />
            <span>记住我</span>
          </label>
        </div>
        <button type="submit" class="login-btn">登 录</button>
        <div class="login-bottom">
          还没有账号？<router-link to="/register" class="register-link">立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const username = ref('')
const password = ref('')
const remember = ref(false)
const router = useRouter()

async function login() {
  try {
    const res = await axios.post('http://localhost:8080/api/users/login', {
      username: username.value,
      password: password.value
    })
    
    localStorage.setItem('user', JSON.stringify(res.data))
    localStorage.setItem('user', JSON.stringify(res.data))
    localStorage.setItem('userRole', res.data.role)
    
    alert('登录成功！')
    router.push('/customer')
  } catch (err) {
    console.error('登录错误:', err)
    if (err.response?.status === 401) {
      alert('登录失败：用户名或密码错误')
    } else {
      alert('登录失败：网络错误，请稍后重试')
    }
  }
}
</script>



<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #2fd7b2 0%, #16a085 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(44, 62, 80, 0.12);
  padding: 48px 40px 32px 40px;
  width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-icon {
  width: 72px;
  height: 72px;
  background: #2fd7b2;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 24px;
  font-size: 36px;
  color: #fff;
  box-shadow: 0 2px 8px rgba(44, 62, 80, 0.10);
}
.login-title {
  font-size: 28px;
  color: #222;
  font-weight: bold;
  margin-bottom: 8px;
  text-align: center;
}
.login-subtitle {
  color: #888;
  font-size: 15px;
  margin-bottom: 28px;
  text-align: center;
}
.input-group {
  position: relative;
  width: 100%;
  margin-bottom: 22px;
}
.input-group i {
  position: absolute;
  left: 18px;
  top: 50%;
  transform: translateY(-50%);
  color: #16a085;
  font-size: 20px;
}
.input-group input {
  width: 100%;
  padding: 14px 14px 14px 48px;
  border: 1.5px solid #e0e0e0;
  border-radius: 8px;
  outline: none;
  font-size: 17px;
  background: #f8f8f8;
  transition: border 0.2s;
}
.input-group input:focus {
  border: 1.5px solid #16a085;
  background: #fff;
}
.login-options {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 22px;
  font-size: 15px;
  color: #888;
}
.remember-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  cursor: pointer;
  user-select: none;
}
.remember-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: #16a085;
  margin-right: 2px;
}
.login-options .forgot {
  color: #16a085;
  text-decoration: none;
  font-size: 15px;
}
.login-options .forgot:hover {
  text-decoration: underline;
}
.login-btn {
  width: 100%;
  background: linear-gradient(90deg, #2fd7b2 0%, #16a085 100%);
  color: #fff;
  border: none;
  padding: 15px 0;
  border-radius: 10px;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(44, 62, 80, 0.10);
  transition: background 0.2s;
  letter-spacing: 2px;
}
.login-btn:hover {
  background: linear-gradient(90deg, #16a085 0%, #2fd7b2 100%);
}
.login-bottom {
  text-align: center;
  color: #888;
  font-size: 15px;
  margin-top: 8px;
}
.register-link {
  color: #16a085;
  text-decoration: none;
  font-weight: bold;
  margin-left: 4px;
}
.register-link:hover {
  text-decoration: underline;
}
</style>
